<?php
class ProductController extends Controller
{
    public function actionIndex()
    {
        $this->render('index');
    }

    public function actionGetdata(){

        $page = $_REQUEST['page'];
        $rows = $_REQUEST['rows'];
        $start = ($page-1)*$rows;
        $total = $this->connection->createCommand("select count(1) from crm_product where deleted = 0")->queryScalar();
        $result = $this->connection->createCommand("select f.*,c.productcategoryname from crm_product f LEFT JOIN crm_productcategory c on f.productcategory_id = c.productcategory_id where f.deleted = 0 limit {$rows} offset {$start}")->queryAll();

        $data = Array();
        $data['total'] = $total;
        $data['rows'] = $result;

        echo json_encode($data);
    }

    public function actionShowedit(){

        $product_id = $_REQUEST['product_id'];

        $product = null;
        if($product_id==0){
            //新增
        }else{
            //编辑
            $product = $this->connection->createCommand("select * from crm_product where product_id = :product_id")->bindParam(':product_id',$product_id,PDO::PARAM_INT)->queryRow();
        }

        $this->render('showedit',['product'=>$product]);
    }

    public function actionSavedata(){

        $product_id = $_REQUEST['product_id'];
        $productname = $_REQUEST['productname'];
        $productcategory_id = $_REQUEST['productcategory_id'];
        $unit = $_REQUEST['unit'];
        $model = $_REQUEST['model'];
        $weight = $_REQUEST['weight'];
        $unitprice = $_REQUEST['unitprice'];
        $costprice = $_REQUEST['costprice'];
        $saleprice = $_REQUEST['saleprice'];
        $description = $_REQUEST['description'];

        if($product_id!=null){
            //编辑
            $this->connection->createCommand("update crm_product set productname = :productname,productcategory_id=:productcategory_id,unit = :unit,model=:model,
  weight=:weight,unitprice=:unitprice,costprice=:costprice,saleprice=:saleprice,description=:description
 where product_id = :product_id")
                ->bindParam(':product_id',$product_id,PDO::PARAM_INT)
                ->bindParam(':productcategory_id',$productcategory_id,PDO::PARAM_INT)
                ->bindParam(':productname',$productname,PDO::PARAM_STR)
                ->bindParam(':unit',$unit,PDO::PARAM_STR)
                ->bindParam(':model',$model,PDO::PARAM_STR)
                ->bindParam(':weight',$weight,PDO::PARAM_STR)
                ->bindParam(':unitprice',$unitprice,PDO::PARAM_STR)
                ->bindParam(':costprice',$costprice,PDO::PARAM_STR)
                ->bindParam(':saleprice',$saleprice,PDO::PARAM_STR)
                ->bindParam(':description',$description,PDO::PARAM_STR)
                ->execute();
        }else{
            //添加
            $this->connection->createCommand("insert into crm_product (productname,productcategory_id,unit,model,weight,unitprice,costprice,saleprice,description) 
values (:productname,:productcategory_id,:unit,:model,:weight,:unitprice,:costprice,:saleprice,:description) ")
                ->bindParam(':productcategory_id',$productcategory_id,PDO::PARAM_INT)
                ->bindParam(':productname',$productname,PDO::PARAM_STR)
                ->bindParam(':unit',$unit,PDO::PARAM_STR)
                ->bindParam(':model',$model,PDO::PARAM_STR)
                ->bindParam(':weight',$weight,PDO::PARAM_STR)
                ->bindParam(':unitprice',$unitprice,PDO::PARAM_STR)
                ->bindParam(':costprice',$costprice,PDO::PARAM_STR)
                ->bindParam(':saleprice',$saleprice,PDO::PARAM_STR)
                ->bindParam(':description',$description,PDO::PARAM_STR)
                ->execute();
        }
    }

    //选择产品
    public function actionSelect(){
        $form = $_REQUEST['form'];
        $fieldname = $_REQUEST['fieldname'];
        $fieldid = $_REQUEST['fieldid'];

        $this->render('select',['form'=>$form,'fieldname'=>$fieldname,'fieldid'=>$fieldid]);
    }

    public function actionRemove(){
        $product_id = $_REQUEST['product_id'];
        $this->connection->createCommand("update crm_product set deleted = 1 
where product_id = :product_id")
            ->bindParam(':product_id',$product_id,PDO::PARAM_INT)
            ->execute();
    }

    public function actionView(){

        $product_id = $_REQUEST['product_id'];
        $product = $this->connection->createCommand("select f.*,c.productcategoryname from crm_product f LEFT JOIN crm_productcategory c on f.productcategory_id = c.productcategory_id where f.product_id = :product_id")->bindParam(':product_id',$product_id,PDO::PARAM_INT)->queryRow();

        $this->render('view',['product'=>$product]);
    }

}